﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Bll;
using System.Data;
using AspNetCs.Common;
using AspNetCs.Model;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.VoteEnum;
public partial class Master_admin_vote : Admin_BasePage
{
    protected Vote vote = new Vote();
    protected string Action = "";
    protected string TextChange = "添加";

    protected void Page_Load(object sender, EventArgs e)
    {
        IsExit();
        if (!Page.IsPostBack)
        {
            this.MultiView1.ActiveViewIndex = 0;
            bind();
            VoteTypeBind();
        }

        if (!string.IsNullOrEmpty(Request["action"]))
        {
            Action = StringHelper.HtmlEncrypt(Request["action"].ToString()).ToLower();
        }
        switch (Action)
        {
            case "add":
                this.MultiView1.ActiveViewIndex = 1;
                this.Button1.Visible = false;
                this.Button2.Visible = true;
                break;


        }



    }
    protected void bind()
    {
        DataTable dt = vote.GetDataTable("");
        this.Repeater1.DataSource = dt;
        this.Repeater1.DataBind();

    }
    protected void VoteTypeBind()
    {
        EnumDescription[] Enum = EnumDescription.GetFieldTexts(typeof(VoteTypeEnum));
        VoteType.DataSource = Enum;
        VoteType.DataTextField = "EnumDisplayText";
        VoteType.DataValueField = "EnumValue";
        VoteType.DataBind();

    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (Repeater1.Items.Count == 0)//显示的数据项为0 既是没有数据
            {
                ((Panel)e.Item.FindControl("pl_nothing")).Visible = true;
            }
        }

    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            if (vote.Del(StringHelper.isNumber_int(e.CommandArgument.ToString())) != -1)
            {
                ResultStr("Admin_Vote.Aspx", "调查信息删除成功!");
            }
        }

        if (e.CommandName == "modify")
        {
            this.MultiView1.ActiveViewIndex = 1;
            this.Button1.Visible = true;
            this.Button2.Visible = false;
            TextChange = "修改";
            this.CurrID.Value = e.CommandArgument.ToString();
            Vote_Model mode = vote.GetModel(StringHelper.isNumber_int(e.CommandArgument.ToString()));
            this.Title.Text = mode.Title;
            this.isnew.SelectedValue = mode.IsNew.ToString();

            this.Select1.Text = mode.Select1;
            this.SumNum1.Text = mode.SumNum1.ToString();

            this.Select2.Text = mode.Select2;
            this.SumNum2.Text = mode.SumNum2.ToString();

            this.Select3.Text = mode.Select3;
            this.SumNum3.Text = mode.SumNum3.ToString();

            this.Select4.Text = mode.Select4;
            this.SumNum4.Text = mode.SumNum4.ToString();

            this.Select5.Text = mode.Select5;
            this.SumNum5.Text = mode.SumNum5.ToString();

            this.Select6.Text = mode.Select6;
            this.SumNum6.Text = mode.SumNum6.ToString();

            this.Select7.Text = mode.Select7;
            this.SumNum7.Text = mode.SumNum7.ToString();

            this.Select8.Text = mode.Select8;
            this.SumNum8.Text = mode.SumNum8.ToString();
            if (mode.VoteType.Equals((int)VoteTypeEnum.checkAll))
            {
                this.VoteType.SelectedValue = mode.VoteType.ToString();
            }
            else
            {
                this.VoteType.SelectedValue = ((int)VoteTypeEnum.Radio).ToString();
            }





        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Vote_Model model = new Vote_Model();
        model.Title = StringHelper.HtmlEncrypt(Title.Text);
        model.Select1 = StringHelper.HtmlEncrypt(Select1.Text);
        model.SumNum1 = StringHelper.isNumber_int(SumNum1.Text);
        model.Select2 = StringHelper.HtmlEncrypt(Select2.Text);
        model.SumNum2 = StringHelper.isNumber_int(SumNum2.Text);
        model.Select3 = StringHelper.HtmlEncrypt(Select3.Text);
        model.SumNum3 = StringHelper.isNumber_int(SumNum3.Text);
        model.Select4 = StringHelper.HtmlEncrypt(Select4.Text);
        model.SumNum4 = StringHelper.isNumber_int(SumNum4.Text);
        model.Select5 = StringHelper.HtmlEncrypt(Select5.Text);
        model.SumNum5 = StringHelper.isNumber_int(SumNum5.Text);
        model.Select6 = StringHelper.HtmlEncrypt(Select6.Text);
        model.SumNum6 = StringHelper.isNumber_int(SumNum6.Text);
        model.Select7 = StringHelper.HtmlEncrypt(Select7.Text);
        model.SumNum7 = StringHelper.isNumber_int(SumNum7.Text);
        model.Select8 = StringHelper.HtmlEncrypt(Select8.Text);
        model.SumNum8 = StringHelper.isNumber_int(SumNum8.Text);
        model.IsNew = StringHelper.isNumber_int(isnew.SelectedValue);
        model.Addtime = DateTime.Now;
        model.VoteType = StringHelper.isNumber_int(VoteType.SelectedValue);
        if (vote.Add(model) != -1)
        {
            ResultStr("Admin_Vote.Aspx", "添加成功!");
        }


    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Vote_Model model = new Vote_Model();
        model.Id = StringHelper.isNumber_int(CurrID.Value.ToString());
        model.Title = StringHelper.HtmlEncrypt(Title.Text);
        model.Select1 = StringHelper.HtmlEncrypt(Select1.Text);
        model.SumNum1 = StringHelper.isNumber_int(SumNum1.Text);
        model.Select2 = StringHelper.HtmlEncrypt(Select2.Text);
        model.SumNum2 = StringHelper.isNumber_int(SumNum2.Text);
        model.Select3 = StringHelper.HtmlEncrypt(Select3.Text);
        model.SumNum3 = StringHelper.isNumber_int(SumNum3.Text);
        model.Select4 = StringHelper.HtmlEncrypt(Select4.Text);
        model.SumNum4 = StringHelper.isNumber_int(SumNum4.Text);
        model.Select5 = StringHelper.HtmlEncrypt(Select5.Text);
        model.SumNum5 = StringHelper.isNumber_int(SumNum5.Text);
        model.Select6 = StringHelper.HtmlEncrypt(Select6.Text);
        model.SumNum6 = StringHelper.isNumber_int(SumNum6.Text);
        model.Select7 = StringHelper.HtmlEncrypt(Select7.Text);
        model.SumNum7 = StringHelper.isNumber_int(SumNum7.Text);
        model.Select8 = StringHelper.HtmlEncrypt(Select8.Text);
        model.SumNum8 = StringHelper.isNumber_int(SumNum8.Text);
        model.IsNew = StringHelper.isNumber_int(isnew.SelectedValue);
        model.Addtime = DateTime.Now;
        model.VoteType = StringHelper.isNumber_int(VoteType.SelectedValue);

        if (vote.Update(model) != -1)
        {
            ResultStr("Admin_Vote.Aspx", "修改成功!");
        }

    }
    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "deleteAll")
        {
            string delid = "";
            if (Request["VoteID"] != null)
            {
                delid = Request["VoteID"].ToString();
            }
            if (!string.IsNullOrEmpty(delid))
            {
                if (vote.delAll(delid) != -1)
                {
                    ResultStr("Admin_Vote.Aspx", "调查信息删除成功!");
                }
            }
            else
            {
                ResultStr("Admin_Vote.Aspx", "参数传递错误，请至少选中一条信息进行删除!");
            }
        }
    }
}
